package hfut.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hfut.example.common.CustomException;
import hfut.example.entity.Category;
import hfut.example.entity.Dish;
import hfut.example.entity.Setmeal;
import hfut.example.mapper.CategoryMapper;
import hfut.example.service.CategoryService;
import hfut.example.service.DishService;
import hfut.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据Id删除分类,删除之前需进行判断是否有菜品与此类别存在关联
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int dCount = dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品
        if (dCount>0){
            //说明已经关联菜品
            throw new CustomException("当前菜品关联了菜品,不能删除");
        }
        //查询当前分类是否关联了套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int sCount = setmealService.count(setmealLambdaQueryWrapper);
        if(sCount>0){
            //说明已经关联套餐
            throw new CustomException("当前菜品关联了套餐,不能删除");
        }

        super.removeById(id);
    }
}
